package com.googlecode.jawb.tests.injection;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.junit.Test;

import com.googlecode.jawb.BinaryType;
import com.googlecode.jawb.JAWB;
import com.googlecode.jawb.clues.SetArray;
import com.googlecode.jawb.clues.SetInjection;
import com.googlecode.jawb.clues.SetType;

import static org.junit.Assert.*;

public class Injection {
	@SetType(type = BinaryType.UINT_8)
	private short val1;
	
	@SetType(type = BinaryType.UINT_16)
	private int val2;
	
	@SetType(type = BinaryType.UINT_8)
	@SetArray(staticLength = 3)
	private int[] arr1;
	
	@SetType(type = BinaryType.OBJECT)
	@SetInjection(fields = {"val1", "val2", "arr1"})
	private Container container;
	
	@SetType(type = BinaryType.OBJECT)
	@SetArray(staticLength = 2)
	@SetInjection(fields = {"val1"})
	private Simple[] arr;
	
	@Test()
	public void test() throws IOException {
		Injection result = (Injection) new JAWB().loadFrom(
			new ByteArrayInputStream(
				new byte[]{
					(byte) 0xAA,
					(byte) 0xAA, (byte) 0xBB,
					(byte) 0xAA, (byte) 0xBB, (byte) 0xCC,
					(byte) 0xDD
				}
			),
			Injection.class
		);
		
		assertEquals(result.container.getVal1(), 0xAA);
		assertEquals(result.container.getVal2(), 0xAABB);
		assertArrayEquals(result.container.getArr1(), new int[]{0xAA, 0xBB, 0xCC});
		assertEquals(result.container.another, 0xDD);
		
		assertEquals(result.arr[0].value, 0xAA);
		assertEquals(result.arr[1].value, 0xAA);
	}
}
